<?php

class input {

    /**
     * 获取GET值
     * 
     * @param type $name
     * @param type $type
     */
    public function get($name, $type = 'default') {
        return isset($_GET[$name]) ? $this->totype($_GET[$name], $type) : false;
    }

    /**
     * 获取POST值
     * 
     * @param type $name
     * @param type $type
     * @return type
     */
    public function post($name, $type) {
        return $this->totype($_POST[$name], $type);
    }

    /**
     * 获取COOKIE
     * 
     * @param type $name
     * @param type $type
     * @return type
     */
    public function cookie($name, $type) {
        return $this->totype($_COOKIE[$name], $type);
    }

    /**
     * 获取URL段
     * 
     * @param type $num
     * @param type $type
     * @return string
     */
    public function url($num = 0, $type = false) {
        $path = array_keys($_GET);

        if (!isset($path[0])) {
            if (!empty($default_controller)) {
                $path[0] = $default_controller;
            } else {
                $path[0] = "admin";
            }
        }
        $route = $path[0];
        $route = str_replace('_' . BASE_SUFFIX, '', $route);
        $routeParts = explode("/", $route);
        if (!isset($routeParts[1])) {
            if (!empty($default_controller)) {
                $routeParts[1] = $default_controller;
            } else {
                $routeParts[1] = "index";
            }
        }
        if (!isset($routeParts[2])) {
            if (!empty($default_controller)) {
                $routeParts[2] = $default_controller;
            } else {
                $routeParts[2] = "index";
            }
        }
        return $routeParts[$num];
    }

    /**
     * 强制类型转换
     * 
     * @param type $val
     * @param type $type
     * @return type
     */
    public function totype($val, $type = 'default') {
        $val = (!get_magic_quotes_gpc()) ? addslashes($val) : $val;
        switch ($type) {
            case 'intval':
                $value = intval($val);
                break;
            case 'text':
                $value = trim($val);
                break;
            case 'html':
                $value = htmlspecialchars($val);
                break;
            default :
                $value = $val;
                break;
        }
        return $value;
    }

}
?>